//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Data.Configuration.Design;

namespace EntLibContrib.Query.Configuration.Design
{
	/// <summary>
	/// Represents a command for adding a database query.
	/// This command ensures that the Database Application Block has been configured.
	/// </summary>
	public class AddDataQueryNodeCommand : AddChildNodeCommand
	{
		#region Construction
		/// <summary>
		/// Initialize a new instance of the <see cref="AddDataQueryNodeCommand"/> class with an <see cref="IServiceProvider"/>.
		/// </summary>
		/// <param name="serviceProvider">The mechanism for retrieving a service object; that is, an object that provides custom support to other objects.</param>
		public AddDataQueryNodeCommand(IServiceProvider serviceProvider)
			: base(serviceProvider, typeof(DataQueryNode))
		{
		}
		#endregion

		#region Protected Methods
		/// <summary>
		/// Adds the <see cref="DataQueryNode"/> to the current <see cref="QueryCollectionNode"/>.
		/// If the Data Application Block configuration is not added to the current application, it is also added.
		/// </summary>
		/// <param name="e">An <see cref="EventArgs"/> containing the event data.</param>
		protected override void OnExecuted(EventArgs e)
		{
			base.OnExecuted(e);

			if (null == CurrentHierarchy.FindNodeByType(typeof(DatabaseSectionNode)))
				new AddDatabaseSectionNodeCommand(ServiceProvider).Execute(CurrentHierarchy.RootNode);
		}
		#endregion
	}
}
